package com.zhaolixiang.camera01

import android.content.Context
import android.hardware.Camera
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS
import android.view.SurfaceView
import java.util.jar.Attributes

/**
 * Created by Administrator on 2017/12/27.
 */
class CameraSurfaceView(context:Context,camera2:Camera):SurfaceView(context),SurfaceHolder.Callback {
    private lateinit var surfaceHolder:SurfaceHolder
    private lateinit var camera:Camera
    init {
        camera=camera2
        //初始化
        surfaceHolder=holder
        surfaceHolder.addCallback(this)
        ////设置SurfaceHolder类型  :surface type push buffers
        surfaceHolder.setType(SURFACE_TYPE_PUSH_BUFFERS)
    }
    override fun surfaceCreated(holder: SurfaceHolder?) {
        //该方法调用时表示 surface已经被创建，现在告诉相机在哪里进行画面预览
        camera.setPreviewDisplay(surfaceHolder)
        camera.startPreview()

    }

    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
        //如果你的预览可以改变或旋转，请注意这里的这些事件。
        // 确保在调整或重新格式化之前停止预览。
        //以使用setDisplayOrientation()方法设置预览的方向。为了改变预览方向，
        // 请在surfaceChanged()方法中显示调用Camera#stopPreview()来停止预览，改变方向以后再次调用Camera#stratPreview()启动预览。
        camera.stopPreview()
        camera.setPreviewDisplay(surfaceHolder)
        camera.startPreview()
    }

    override fun surfaceDestroyed(holder: SurfaceHolder?) {
    }


}